﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Common.Other
{
    public static class ExeHelper
    {
        //private const int SW_HIDE = 0; //隐藏任务栏
        //private const int SW_RESTORE = 9;//显示任务栏
        //[DllImport("user32.dll")]
        //public static extern int ShowWindow(int hwnd, int nCmdShow);
        //[DllImport("user32.dll")]
        //public static extern int FindWindow(string lpClassName, string lpWindowName);

        //public static string strPathExe = Environment.CurrentDirectory + "\\NFC_Reader" + "\\MyNFCForm.exe";
        public static Process process = new System.Diagnostics.Process();
        /// <summary>
        /// 打开外部exe子程序
        /// </summary>
        /// <param name="exeName"></param>
        /// <param name="exePath"></param>
        public static void OpenSubExeSoftware(string exeName, string exePath)
        {
            //需要设置startInfo.WorkingDirectory，一般设置为改外部程序的路径，这样改外部程序加载配置文件时才能顺利从改文件夹加载。
            string _workingDirectory = exePath.Replace($"\\{exeName}.exe", "");
            //先检查是否开启，是则关闭
            CloseSubExeSoftware(exeName);

            ////①OpenApplication
            //System.Diagnostics.Process.Start(exePath);
            //process.StartInfo.FileName = exePath;
            //process.Start();//启动

            //②OpenWithStartInfo
            ProcessStartInfo startInfo = new ProcessStartInfo(exePath);
            //startInfo.WindowStyle = ProcessWindowStyle.Minimized;
            startInfo.WorkingDirectory = _workingDirectory;
            Process.Start(startInfo);
            //ShowWindow(FindWindow(exeName, null), SW_HIDE);
            //startInfo.Arguments = "www.northwindtraders.com";
            //Process.Start(startInfo);

        }
        /// <summary>
        /// 关闭外部exe子程序
        /// </summary>
        /// <param name="exeName"></param>
        public static void CloseSubExeSoftware(string exeName)
        {
            System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();//获取所有进程
            foreach (System.Diagnostics.Process p in ps)
            {
                if (p.ProcessName == exeName)
                {
                    //ShowWindow(FindWindow(exeName, null), SW_RESTORE);
                    p.Kill();
                }
            }

            //process.CloseMainWindow();//通过向进程的主窗口发送关闭消息来关闭拥有用户界面的进程
            //process.Close();//释放与此组件关联的所有资源
        }
        /// <summary>
        /// 检查某外部exe子程序是否开启
        /// </summary>
        /// <param name="exeName"></param>
        /// <returns></returns>
        public static bool IsSubExeSoftwareOn(string exeName)
        {
            System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();//获取所有进程
            foreach (System.Diagnostics.Process p in ps)
            {
                if (p.ProcessName == exeName)
                {
                    return true;
                }
            }
            return false;
        }


    }
}
